﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Diagnostics;
using Kiva7.Translations;

namespace Kiva7.Settings
{
    public class AppSettings
    {
        // Our isolated storage settings
        readonly IsolatedStorageSettings _isolatedStore;

        // The isolated storage key names of our settings
        const string ShowBackgroundImageKeyName = "ShowBackgroundImageSetting";
        const string LenderIdKeyName = "LenderIdSetting";
        const string UserIdKeyName = "UserIdSetting";

        const string PasswordKeyName = "PasswordSetting";
        const string TranslationKeyName = "TranslatorOptionSetting";
        const string StatCounterCookieKeyName = "StatCounterCookieSetting";

        // The default value of our settings
        const bool ShowBackgroundImageDefault = true;
        const string LenderIdDefault = "";
        const string UserIdDefault = "";
        const string PasswordDefault = "";
        const string StatCounterCookieDefault = "";
        const TranslatorOption TranslationDefault = TranslatorOption.Never;

        /// <summary>
        /// Constructor that gets the application settings.
        /// </summary>
        public AppSettings()
        {
            try
            {
                // Get the settings for this application.
                _isolatedStore = IsolatedStorageSettings.ApplicationSettings;

            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
            }
        }


        private static AppSettings _current = null;
        public static AppSettings Current
        {
            get
            {
                if (_current == null)
                    _current = new AppSettings();

                return _current;
            }
        }

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool AddOrUpdateValue(string key, Object value)
        {
            bool valueChanged = false;

            try
            {
                // if new value is different, set the new value.
                if (_isolatedStore[key] != value)
                {
                    _isolatedStore[key] = value;
                    valueChanged = true;
                }
            }
            catch (KeyNotFoundException)
            {
                _isolatedStore.Add(key, value);
                valueChanged = true;
            }
            catch (ArgumentException)
            {
                _isolatedStore.Add(key, value);
                valueChanged = true;
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
            }

            return valueChanged;
        }


        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="TValueType"></typeparam>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public TValueType GetValueOrDefault<TValueType>(string key, TValueType defaultValue)
        {
            TValueType value;

            try
            {
                value = (TValueType)_isolatedStore[key];
            }
            catch (KeyNotFoundException)
            {
                value = defaultValue;
            }
            catch (ArgumentException)
            {
                value = defaultValue;
            }

            return value;
        }


        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            _isolatedStore.Save();
        }


        public string LenderIdSetting
        {
            get
            {
                return GetValueOrDefault(LenderIdKeyName, LenderIdDefault);
            }
            set
            {
                AddOrUpdateValue(LenderIdKeyName, value);
                Save();
            }
        }


        public string UserIdSetting
        {
            get
            {
                return GetValueOrDefault(UserIdKeyName, UserIdDefault);
            }
            set
            {
                AddOrUpdateValue(UserIdKeyName, value);
                Save();
            }
        }

        public string PasswordSetting
        {
            get
            {
                return GetValueOrDefault(PasswordKeyName, PasswordDefault);
            }
            set
            {
                AddOrUpdateValue(PasswordKeyName, value);
                Save();
            }
        }

        public TranslatorOption TranslationSetting
        {
            get
            {
                return GetValueOrDefault(TranslationKeyName, TranslationDefault);
            }
            set
            {
                AddOrUpdateValue(TranslationKeyName, value);
                Save();
            }
        }

        public bool ShowBackgroundImage
        {
            get
            {
                return GetValueOrDefault(ShowBackgroundImageKeyName, ShowBackgroundImageDefault);
            }
            set
            {
                AddOrUpdateValue(ShowBackgroundImageKeyName, value);
                Save();
            }
        }

        public string StatCounterCookie
        {
            get
            {
                return GetValueOrDefault(StatCounterCookieKeyName, StatCounterCookieDefault);
            }
            set
            {
                AddOrUpdateValue(StatCounterCookieKeyName, value);
                Save();
            }
        }

    }
}
